package com.example.administrator.biyesheji_print;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.administrator.biyesheji_print.Adapter.MyFragmentAdapter;
import com.example.administrator.biyesheji_print.Fragment.DocumentFragment;
import com.example.administrator.biyesheji_print.Fragment.HomeFragment;
import com.example.administrator.biyesheji_print.Fragment.MyInfoFragment;
import com.example.administrator.biyesheji_print.Fragment.PrintFragment;
import com.example.administrator.biyesheji_print.Fragment.TabDB;
import com.example.administrator.biyesheji_print.Utils.Constant;
import com.example.administrator.biyesheji_print.Utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, TabHost.OnTabChangeListener {


    @BindView(R.id.pager)
    ViewPager pager;
    private Class mFragArr[] = {};

    @BindView(R.id.contentLayout)
    FrameLayout contentLayout;
    @BindView(android.R.id.tabcontent)
    FrameLayout tabcontent;
    public static FragmentTabHost tabhost;
    private List<Fragment> list = new ArrayList<Fragment>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initUI();
    }

    private void initUI() {
        tabhost = findViewById(android.R.id.tabhost);
        pager.addOnPageChangeListener(this);
      //  pager.setPageTransformer(true,new CubeAnim());
        list= new ArrayList<Fragment>();
        list.add(new HomeFragment());
        list.add(new PrintFragment());
        list.add(new DocumentFragment());
        list.add(new MyInfoFragment());
        pager.setOffscreenPageLimit(2);
        pager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),list));
        tabhost.setup(this, getSupportFragmentManager(), R.id.pager);//绑定
        tabhost.setOnTabChangedListener(this);
        String[] tabText = TabDB.getTabText();
        for (int i = 0; i < tabText.length; i++) {
            TabHost.TabSpec tabSpec = tabhost.newTabSpec(tabText[i]).setIndicator(getTabItemView(i));// Tab按钮添加文字和图片
            tabhost.addTab(tabSpec, TabDB.getTabClass()[i], null);// 设置Tab按钮的背景
        }



    }


    // 获得图片资源
    private View getTabItemView(int i) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        ImageView iv_tab = view.findViewById(R.id.iv_tab);
        TextView tv_tab = view.findViewById(R.id.tv_tab);
        iv_tab.setImageResource(TabDB.getTabImg()[i]);
        tv_tab.setText(TabDB.getTabText()[i]);
        return view;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {//position是表示你当前选中的页面位置Postion，这事件是在你页面跳转完毕的时候调用的。
        TabWidget widget = tabhost.getTabWidget();
        int oldFocusability = widget.getDescendantFocusability();
        widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);//设置View覆盖子类控件而直接获得焦点
        tabhost.setCurrentTab(position);//根据位置Postion设置当前的Tab
        widget.setDescendantFocusability(oldFocusability);//设置取消分割线


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabChanged(String tabId) {
        int position = tabhost.getCurrentTab();
        pager.setCurrentItem(position);//把选中的Tab的位置赋给适配器，让它控制页面切换
    }
//    public String gettype(){
//      return SharedPreferencesUtils.getStringValue(getApplicationContext(), Constant.TYPE, "");
//    }
}
